package com.bustamanteortega.diagnosticopds;

import android.os.Bundle;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import net.*;

public class LoginActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_login, menu);
        return true;
    }
    
    
    public void autentificar(View v){
    	
    	EditText nombre = (EditText) findViewById(R.id.editTextDNI);
    	
    	String url = "http://10.0.2.2:8787/pds/pacientes/" + nombre.getText().toString();
		RestClient client = new RestClient(url);
		try {
			client.execute(RequestMethod.GET);
			if(client.getResponseCode() == 200) {
				// guardar info
		    	SharedPreferences mySharedPreferences =  getSharedPreferences("USER_PREFS", Activity.MODE_PRIVATE);
		    	SharedPreferences.Editor editor = mySharedPreferences.edit();

		    	editor.putString("RUT",  nombre.getText().toString());
		    	editor.commit();
		    	
				//Successfully connected
				Intent myIntent = new Intent(LoginActivity.this, Principal.class);
		    	LoginActivity.this.startActivity(myIntent);
		    	
		    	
		    	
		    	finish();

			} else if(client.getResponseCode() == 404){
				//error connecting to server, lets just return an error
				Toast.makeText(getApplicationContext(), "Usuario no registrado", Toast.LENGTH_SHORT).show();
	
			}else{
				Toast.makeText(getApplicationContext(), "Error, no se puede conectar" + url, Toast.LENGTH_SHORT).show();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
}
